package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.IndustryMapInfoMapper;
import cc.rengu.igas.bomp.common.entity.IndustryMapInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: zhangxuran
 * @Date: 2020/5/6 9:58
 * @Description: 行业类别映射关系表
 */
public class IndustryMapInfoMapperImpl implements IndustryMapInfoMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);


    @Override
    public IndustryMapInfo selectIndustryMapInfoByIndustryTypeAndChannelType(String industryType, String channelType, String publicPrivateFlag) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectIndustryMapInfoByIndustryTypeAndChannelType");
        IndustryMapInfo industryMapInfo = new IndustryMapInfo();
        industryMapInfo.setIndustryType(industryType);
        industryMapInfo.setDestChannel(channelType);
        industryMapInfo.setPublicPrivateFlag(publicPrivateFlag);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, industryMapInfo, industryMapInfo);
        if (0 != iReturnCode) {
            rglog.error("获取行业类别映射关系信息失败, industryType:<{}>,channelType:<{}>,publicPrivateFlag:<{}>,iReturnCode:<{}>",
                    industryType, channelType, publicPrivateFlag, iReturnCode);
            return null;
        }
        return industryMapInfo;
    }


    @Override
    public List<IndustryMapInfo> selectIndustryMapInfoByCondition(String industryType, String channelType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectIndustryMapInfoByCondition");
        IndustryMapInfo industryMapInfo = new IndustryMapInfo();
        industryMapInfo.setIndustryType(industryType);
        industryMapInfo.setDestChannel(channelType);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, industryMapInfo, IndustryMapInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取行业类别映射关系信息失败, industryType:<{}>,channelType:<{}>,",
                    industryType, channelType);
            return null;
        }
        return objectList.stream().map(item -> (IndustryMapInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<IndustryMapInfo> selectIndustryMapInfoByDestCatalogCd(String destChannel, String destCatalogCd) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectIndustryMapInfoByDestCatalogCd");
        IndustryMapInfo industryMapInfo = new IndustryMapInfo();
        industryMapInfo.setDestChannel(destChannel);
        industryMapInfo.setDestCatalogCd(destCatalogCd);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, industryMapInfo, IndustryMapInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取行业类别映射关系信息失败, destChannel:<{}>,destCatalogCd:<{}>,", destChannel, destCatalogCd);
            return null;
        }
        return objectList.stream().map(item -> (IndustryMapInfo) item).collect(Collectors.toList());
    }
}
